package com.frxuexi.docments

import android.os.Environment
import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.BaseApp
import com.frxuexi.lib_base.RequestType
import com.frxuexi.lib_base.UiState
import com.frxuexi.lib_base.entity.LocalDocEntity
import kotlinx.coroutines.launch
import kotlin.math.log10

@Composable
fun LocalDocPage(navHostController: NavHostController,vm:DocViewModel= hiltViewModel()) {

    var list = remember {
        SnapshotStateList<LocalDocEntity>()
    }
    val dir = BaseApp.context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)


    LaunchedEffect(key1 = "", block = {
        launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        if(it.type==RequestType.SCAN){
                            list.addAll(it.data as Collection<LocalDocEntity>)
                        }else if(it.type==RequestType.UPLOAD){
                            ToastUtils.showLong("上传成功")
                            navHostController.popBackStack()
                            vm.sendIntent(DocIntent.getDoc(mapOf(Pair("userId", 1), Pair("page", 1))))
                        }
                    }
                    is UiState.onError->{
                        Log.i("===",it.msg)
                    }
                    else->{}
                }
            }
        }
    })

    vm.sendIntent(DocIntent.scanFiles(dir!!))//发送扫描本地文件意图

    var chName by remember {
        mutableStateOf("")
    }
    var chPath by remember {
        mutableStateOf("")
    }
    Column(verticalArrangement = Arrangement.spacedBy(10.dp)) {
        Row(modifier = Modifier
            .fillMaxWidth()
            .padding(10.dp), horizontalArrangement = Arrangement.SpaceBetween) {
            Icon(imageVector = Icons.Default.ArrowBack, contentDescription = "",
                Modifier.clickable { navHostController.popBackStack() })
            Text(text = "上传文件选择", textAlign = TextAlign.Center)
            Text(text = "上传", Modifier.clickable {
                vm.sendIntent(DocIntent.uploadFile(chName,chPath))
            })
        }
        LazyColumn(Modifier.padding(horizontal = 10.dp),content = {
            items(list){
                LocalDocItem(it,chName==it.fileName){
                    chName=it.fileName
                    chPath=it.path
                }
            }
        })
    }
}

@Composable
fun LocalDocItem(entity: LocalDocEntity,checked:Boolean,onCheck:()->Unit){
    /*var checked by remember {
        mutableStateOf(false)
    }
    var chName by remember {
        mutableStateOf("")
    }*/
    Row() {
        
        Box(contentAlignment= Alignment.TopEnd, modifier = Modifier.padding(10.dp)) {
            Image(painter = painterResource(id = R.drawable.icon3), contentDescription = "",Modifier.size(50.dp))

            Image(painter = painterResource(id = if(checked) R.drawable.ic_action_check else R.drawable.ic_action_uncheck), contentDescription = "",
                Modifier
                    .size(15.dp)
                    .offset(x = 5.dp, y = -10.dp)
                    .clickable {
                        onCheck()
//                        checked=true//多选
//                        checked=!checked//反选
//                        chName=entity.fileName
//                        ToastUtils.showLong(checked.toString())
                    })
        }
        Column(verticalArrangement = Arrangement.SpaceAround) {
            Text(text = entity.fileName)
            Text(text = entity.path, color = Color.LightGray, fontSize = 16.sp,maxLines=1,overflow= TextOverflow.Ellipsis)
        }

    }
}